package com.wzz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzz.dto.TikuVo;
import com.wzz.dto.UserVo;
import com.wzz.entity.*;
import com.wzz.service.*;
import com.wzz.utils.Excel.ExcelUtils;
import com.wzz.utils.SaltEncryption;
import com.wzz.vo.CommonResult;
import com.wzz.vo.PageResponse;
import com.wzz.vo.QuestionVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author by liutao
 * @implNote 10/20 19:07
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/excel")
public class ExcelController {


    private final QuestionService questionService;

    private final AnswerService answerService;

    private final UserService userService;

    private final QuestionBankService questionBankService;


    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        try {
//            final List<Shop> list = shopService.list();
//            // List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response
//            ExcelUtils.exportExcel(list, "店铺列表", "店铺数据", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file,String createPerson) {

        final List<TikuVo> shops = ExcelUtils.importExcel(file, 1, 1, TikuVo.class);

        for (TikuVo shop : shops) {
            String opt = "";
            String bname = "";
            Question question = new Question();
            question.setQuContent(shop.getContent());
            question.setCreateTime(new Date());
            question.setCreatePerson("admin");
            question.setQuType(Integer.valueOf(shop.getLy()));
            question.setLevel(Integer.valueOf(shop.getLevel()));
            question.setQuBankId(shop.getFl());
            question.setAnalysis(shop.getAnalysis());
            question.setQuBankName(shop.getFl());
            String fl = shop.getFl();
            String[] split = fl.split(",");
            for (String string : split) {
                QuestionBank byId = questionBankService.getById(Integer.valueOf(string));
                bname+=byId.getBankName()+",";
            }
            question.setQuBankName(bname.substring(0,bname.length()-1));
            questionService.save(question);
            Answer answer = new Answer();
            answer.setQuestionId(question.getId());
            if(shop.getXxa() != null) {
                opt+=shop.getXxa()+",";
            }
            if(shop.getXxa() != null) {
                opt+=shop.getXxb()+",";
            }
            if(shop.getXxa() != null) {
                opt+=shop.getXxc()+",";
            }
            if(shop.getXxa() != null) {
                opt+=shop.getXxd()+",";
            }
            answer.setImages("");
            answer.setAllOption(opt.substring(0,opt.length()-1));
            answer.setTrueOption(shop.getDa());
            answerService.save(answer);
        }
    }

    @PostMapping("/importExcelUser")
    public void importExcelUser(@RequestPart("file") MultipartFile file,String createPerson) {

        final List<UserVo> userVos = ExcelUtils.importExcel(file, 1, 1, UserVo.class);

        for (UserVo userVo : userVos) {
            if(StringUtils.isBlank(userVo.getUsername())){
                continue;
            }
            User user = new User();
            BeanUtils.copyProperties(userVo, user);
            user.setCreateDate(new Date());
            user.setPassword("123456");
            user.setRoleId(1);
            String salt = UUID.randomUUID().toString().substring(0, 6);
            String newPwd = SaltEncryption.saltEncryption(user.getPassword(), salt);
            user.setPassword(newPwd);
            user.setSalt(salt);
            userService.save(user);
        }
    }

}
